import { request } from "../request"
import { Toast } from "vant"
import router from '@/router/index'
// 注册接口
const registerRequest = async userInfo => {
  const res = await request({
    url: '/users/register',
    method: 'post',
    data: {
      user: userInfo.user,
      password: userInfo.password
    }
  })
}
// 手机号登录接口
const loginMobileRequest = async user => {
  try {
    const res = await request({
      url: "/users/login_mobile",
      method: 'post',
      data: {
        user: user
      }
    })
    return res
  } catch (error) {
    console.log(error);
  }
}
// token 验证
const verifyToken = async token => {
  try {
    const res = await request({
      url: '/users/auth'
    })
    return true
  } catch (error) {
    console.log(error);
    if (error.response.status === 401) {
      Toast.loading({
        message: '登录已过期...',
        forbidClick: true,
        loadingType: 'spinner',
      });
      setTimeout(() => {
        router.replace('/profile')
      }, 1000)
    }
  }
}

export {
  registerRequest,
  loginMobileRequest,
  verifyToken
}